<template>
	<view class="addVisitorsBox">
		<view class="visitorsInfo">
			<view class="title">药品编辑</view>
			
			<u-form :model="form" ref="uForm" label-width="130">
				<u-form-item label="名称" ><u-input v-model="form.medName" /></u-form-item>
				<u-form-item label="说明"><u-input type="textarea" v-model="form.content" /></u-form-item>
				<u-form-item label="规格"><u-input v-model="form.spec" /></u-form-item>
				<u-form-item label="用量"><u-input v-model="form.usageText" /></u-form-item>
				<u-form-item label="价格"><u-input v-model="form.price" /></u-form-item>
				<u-form-item label="图片">
					<view class="u-flex u-direction-row u-p-l-25 u-p-b-25 ">
						          <u-image border-radius="15" @tap="change('pic')" class="u-image-shadow u-p-r-30" height="120rpx"  
							width="120rpx" :src="getPic(form.pic)" mode="aspectFill" >
					           		<view slot="error" style="font-size: 24rpx;" class="u-text-center">
						             	<u-icon size="30" name="plus"></u-icon><br />选择图片
						            </view>
					           		<view slot="loading" style="font-size: 24rpx;" class="u-text-center">
						             	<u-icon size="30" name="reload"></u-icon><br />加载中
						            </view>
									</u-image> 
								</view>		
				</u-form-item>
				
			</u-form>
			
		</view>
		
		<view class="addButton" @click="confirm">
			提交
		</view>
	</view>
</template>

<script>
	import appRequest from "@/common/appRequestUrl.js";
	import base64 from "@/common/base64.js"
	export default {
		data() {
			return {
				
				type:2,
				id:0,
				password:"",
				form:{
					medName:"",
					content:"",
					spec:"",
					usageText:"",
					price:"",
					pic:"nuoMi.png",
				},
				//默认建档方式
				filingWay: 0,
				//是否显示号码类型选择
				showCardType: false,
				//号码类型
				cardTypeList: [
					['就诊卡', '医保卡']
				],
				//绑卡类型
				cardType: null,
				//就诊人信息
				userInfo: {

				},
				picPath:appRequest.urlMap.getPic,
				current:0,
				user:{}
			}
		},
		onLoad:function(options){
			this.user = appRequest.getUserInfo();
			if(!this.user){
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			if(options.id){
				this.id = options.id;
				this.getMed();
			}
		},
		methods: {

			getPic(pic){
				if(pic.length > 100){
					return pic;
				}else{
					return this.picPath + pic;
				}
				
			},
			getMed(){
				let _this = this;
				appRequest.request({
					method: "POST",
					data: {
						id:this.id,
						validFLag:1
					},
					url: appRequest.urlMap.findNmHospitalMedicine,
					success: function(res) {
						if(res.data.code == 200){
							_this.form = res.data.data;
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
			},
			confirm() {
				if(!this.form.medName || 
				!this.form.content||
				!this.form.spec||
				!this.form.usageText||
				!this.form.price){
					_this.$api.msg("请填写完整");
					return;
				}
				
				let _this = this;
				appRequest.request({
					method: "POST",
					data: this.form,
					url: appRequest.urlMap.editNmHospitalMedicine,
					success: function(res) {
						if(res.data.code == 200){
							_this.$api.msg("提交成功");
							uni.navigateBack();
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
			},
			change(name) {
				let _this = this;
				uni.chooseImage({
					count: 1,
					sizeType: ["compressed"],
					success: function(res) {
						let src = res.tempFiles[0].path;
						uni.compressImage({
							src: src,
							width: 450,
							quality: 70,
							success: function(path) {
								let filePath = path.tempFilePath;
								wx.getFileSystemManager().readFile({
									filePath: filePath,
									encoding: 'base64',
									fail: function(errMsg) {
										console.log(errMsg);
									},
									success: function(res) {
										_this.form[name] =
											"data:image/jpeg;base64," + res.data;
									}
								});
							}
						});
					},
					fail: function(errMsg) {
						console.log(errMsg);
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.addVisitorsBox {
		padding:30rpx 40rpx;
		.visitorsInfo{
			.title{
				font-weight: 400;
				line-height: 56rpx;
				color: #343434;
				font-size: 40rpx;
			}
			.value{
				margin-top: 30rpx;
			}
		}
		.filingWay{
			font-weight: 400;
			line-height: 56rpx;
			color: #343434;
			margin-top: 30rpx;
			font-size: 40rpx;
			.cardType{
				display: flex;
				justify-content: space-between;
				padding: 9rpx 6rpx;
				font-size: 30rpx;
				margin-top: 30rpx;
				color: #CBCBCB;
				border: 1px solid #E4E4E2;
				border-radius: 8rpx;
			}
			.value{
				margin-top: 30rpx;
			}
		}
		.addButton {
			width: 490rpx;
			height: 84rpx;
			margin: 100rpx auto 0;
			background: linear-gradient(90deg, #5088FD 0%, #62B9F9 100%);
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 40rpx;
			letter-spacing: 4rpx;
		}
	}
</style>
